package com.devil.traffic.activity;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.devil.traffic.service.TrafficService;
import com.devil.traffic.task.Task;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

public class ResultActivity extends Activity {

	private LinearLayout progress;
	private LinearLayout failture;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//把activity压入栈内
		TrafficService.allActivities.add(this);
		//设置布局文件
		setContentView(R.layout.result_activity);
		//数据查询缓冲进度条
		progress = (LinearLayout) findViewById(R.id.progress);
		//查询失败内容
		failture = (LinearLayout) findViewById(R.id.failture);
		//显示查询进度条
		showLoading(true);
	}
	
	//刷新UI方法
	public void refresh(Object...param){
		//隐藏查询进度条
		showLoading(false);
		int flag = ((Integer)param[0]).intValue();
		if(flag==Task.GET_SIMPLE_TRAFFIC_RECORD){
			System.out.println("准备刷新界面");
			//存储查询返回的网络数据
			String data = (String) param[1];
			if(data!=null){
				//把服务器返回的数据存放到document类中
				Document document = Jsoup.parse(data);
				Elements fonts = document.select("font");
				Elements content = fonts.get(8).select("font");
				System.out.println("您查询的"+content.get(1).html().toString()
						+"共有"+content.get(2).html().toString()+"条违法记录");
				Toast.makeText(ResultActivity.this, "您查询的"+content.get(1).html().toString()
						+"共有"+content.get(2).html().toString()+"条违法记录", Toast.LENGTH_LONG).show();
			}
		}else{
			failture.setVisibility(View.VISIBLE);
			Toast.makeText(ResultActivity.this, "获取网络数据失败，请稍后重试！", Toast.LENGTH_SHORT).show();
		}
	}
	
	//进度条显隐控制
	public void showLoading(Boolean isShow){
		if(progress!=null){
			progress.setVisibility(isShow ? View.VISIBLE : View.GONE);
		}
	}
	
}
